{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 3.2 \u6267\u884c\u7cbe\u786e\u7684\u6d6e\u70b9\u6570\u8fd0\u7b97\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u9700\u8981\u5bf9\u6d6e\u70b9\u6570\u6267\u884c\u7cbe\u786e\u7684\u8ba1\u7b97\u64cd\u4f5c\uff0c\u5e76\u4e14\u4e0d\u5e0c\u671b\u6709\u4efb\u4f55\u5c0f\u8bef\u5dee\u7684\u51fa\u73b0\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6d6e\u70b9\u6570\u7684\u4e00\u4e2a\u666e\u904d\u95ee\u9898\u662f\u5b83\u4eec\u5e76\u4e0d\u80fd\u7cbe\u786e\u7684\u8868\u793a\u5341\u8fdb\u5236\u6570\u3002\n\u5e76\u4e14\uff0c\u5373\u4f7f\u662f\u6700\u7b80\u5355\u7684\u6570\u5b66\u8fd0\u7b97\u4e5f\u4f1a\u4ea7\u751f\u5c0f\u7684\u8bef\u5dee\uff0c\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a = 4.2\nb = 2.1\na + b"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "(a + b) == 6.3"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e9b\u9519\u8bef\u662f\u7531\u5e95\u5c42CPU\u548cIEEE 754\u6807\u51c6\u901a\u8fc7\u81ea\u5df1\u7684\u6d6e\u70b9\u5355\u4f4d\u53bb\u6267\u884c\u7b97\u672f\u65f6\u7684\u7279\u5f81\u3002\n\u7531\u4e8ePython\u7684\u6d6e\u70b9\u6570\u636e\u7c7b\u578b\u4f7f\u7528\u5e95\u5c42\u8868\u793a\u5b58\u50a8\u6570\u636e\uff0c\u56e0\u6b64\u4f60\u6ca1\u529e\u6cd5\u53bb\u907f\u514d\u8fd9\u6837\u7684\u8bef\u5dee\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u66f4\u52a0\u7cbe\u786e(\u5e76\u80fd\u5bb9\u5fcd\u4e00\u5b9a\u7684\u6027\u80fd\u635f\u8017)\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 decimal \u6a21\u5757\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from decimal import Decimal\na = Decimal('4.2')\nb = Decimal('2.1')\na + b"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print(a + b)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "(a + b) == Decimal('6.3')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u521d\u770b\u8d77\u6765\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u597d\u50cf\u6709\u70b9\u5947\u602a\uff0c\u6bd4\u5982\u6211\u4eec\u7528\u5b57\u7b26\u4e32\u6765\u8868\u793a\u6570\u5b57\u3002\n\u7136\u800c\uff0c Decimal \u5bf9\u8c61\u4f1a\u50cf\u666e\u901a\u6d6e\u70b9\u6570\u4e00\u6837\u7684\u5de5\u4f5c(\u652f\u6301\u6240\u6709\u7684\u5e38\u7528\u6570\u5b66\u8fd0\u7b97)\u3002\n\u5982\u679c\u4f60\u6253\u5370\u5b83\u4eec\u6216\u8005\u5728\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u51fd\u6570\u4e2d\u4f7f\u7528\u5b83\u4eec\uff0c\u770b\u8d77\u6765\u8ddf\u666e\u901a\u6570\u5b57\u6ca1\u4ec0\u4e48\u4e24\u6837\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "decimal \u6a21\u5757\u7684\u4e00\u4e2a\u4e3b\u8981\u7279\u5f81\u662f\u5141\u8bb8\u4f60\u63a7\u5236\u8ba1\u7b97\u7684\u6bcf\u4e00\u65b9\u9762\uff0c\u5305\u62ec\u6570\u5b57\u4f4d\u6570\u548c\u56db\u820d\u4e94\u5165\u8fd0\u7b97\u3002\n\u4e3a\u4e86\u8fd9\u6837\u505a\uff0c\u4f60\u5148\u5f97\u521b\u5efa\u4e00\u4e2a\u672c\u5730\u4e0a\u4e0b\u6587\u5e76\u66f4\u6539\u5b83\u7684\u8bbe\u7f6e\uff0c\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from decimal import localcontext\na = Decimal('1.3')\nb = Decimal('1.7')\nprint(a / b)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with localcontext() as ctx:\n    ctx.prec = 3\n    print(a / b)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with localcontext() as ctx:\n    ctx.prec = 50\n    print(a / b)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "decimal \u6a21\u5757\u5b9e\u73b0\u4e86IBM\u7684\u201d\u901a\u7528\u5c0f\u6570\u8fd0\u7b97\u89c4\u8303\u201d\u3002\u4e0d\u7528\u8bf4\uff0c\u6709\u5f88\u591a\u7684\u914d\u7f6e\u9009\u9879\u8fd9\u672c\u4e66\u6ca1\u6709\u63d0\u5230\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Python\u65b0\u624b\u4f1a\u503e\u5411\u4e8e\u4f7f\u7528 decimal \u6a21\u5757\u6765\u5904\u7406\u6d6e\u70b9\u6570\u7684\u7cbe\u786e\u8fd0\u7b97\u3002\n\u7136\u800c\uff0c\u5148\u7406\u89e3\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u76ee\u7684\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\n\u5982\u679c\u4f60\u662f\u5728\u505a\u79d1\u5b66\u8ba1\u7b97\u6216\u5de5\u7a0b\u9886\u57df\u7684\u8ba1\u7b97\u3001\u7535\u8111\u7ed8\u56fe\uff0c\u6216\u8005\u662f\u79d1\u5b66\u9886\u57df\u7684\u5927\u591a\u6570\u8fd0\u7b97\uff0c\n\u90a3\u4e48\u4f7f\u7528\u666e\u901a\u7684\u6d6e\u70b9\u7c7b\u578b\u662f\u6bd4\u8f83\u666e\u904d\u7684\u505a\u6cd5\u3002\n\u5176\u4e2d\u4e00\u4e2a\u539f\u56e0\u662f\uff0c\u5728\u771f\u5b9e\u4e16\u754c\u4e2d\u5f88\u5c11\u4f1a\u8981\u6c42\u7cbe\u786e\u5230\u666e\u901a\u6d6e\u70b9\u6570\u80fd\u63d0\u4f9b\u768417\u4f4d\u7cbe\u5ea6\u3002\n\u56e0\u6b64\uff0c\u8ba1\u7b97\u8fc7\u7a0b\u4e2d\u7684\u90a3\u4e48\u4e00\u70b9\u70b9\u7684\u8bef\u5dee\u662f\u88ab\u5141\u8bb8\u7684\u3002\n\u7b2c\u4e8c\u70b9\u5c31\u662f\uff0c\u539f\u751f\u7684\u6d6e\u70b9\u6570\u8ba1\u7b97\u8981\u5feb\u7684\u591a-\u6709\u65f6\u5019\u4f60\u5728\u6267\u884c\u5927\u91cf\u8fd0\u7b97\u7684\u65f6\u5019\u901f\u5ea6\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5373\u4fbf\u5982\u6b64\uff0c\u4f60\u5374\u4e0d\u80fd\u5b8c\u5168\u5ffd\u7565\u8bef\u5dee\u3002\u6570\u5b66\u5bb6\u82b1\u4e86\u5927\u91cf\u65f6\u95f4\u53bb\u7814\u7a76\u5404\u7c7b\u7b97\u6cd5\uff0c\u6709\u4e9b\u5904\u7406\u8bef\u5dee\u4f1a\u6bd4\u5176\u4ed6\u65b9\u6cd5\u66f4\u597d\u3002\n\u4f60\u4e5f\u5f97\u6ce8\u610f\u4e0b\u51cf\u6cd5\u5220\u9664\u4ee5\u53ca\u5927\u6570\u548c\u5c0f\u6570\u7684\u52a0\u5206\u8fd0\u7b97\u6240\u5e26\u6765\u7684\u5f71\u54cd\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "nums = [1.23e+18, 1, -1.23e+18]\nsum(nums) # Notice how 1 disappears"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0a\u9762\u7684\u9519\u8bef\u53ef\u4ee5\u5229\u7528 math.fsum() \u6240\u63d0\u4f9b\u7684\u66f4\u7cbe\u786e\u8ba1\u7b97\u80fd\u529b\u6765\u89e3\u51b3\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import math\nmath.fsum(nums)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7136\u800c\uff0c\u5bf9\u4e8e\u5176\u4ed6\u7684\u7b97\u6cd5\uff0c\u4f60\u5e94\u8be5\u4ed4\u7ec6\u7814\u7a76\u5b83\u5e76\u7406\u89e3\u5b83\u7684\u8bef\u5dee\u4ea7\u751f\u6765\u6e90\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u603b\u7684\u6765\u8bf4\uff0c decimal \u6a21\u5757\u4e3b\u8981\u7528\u5728\u6d89\u53ca\u5230\u91d1\u878d\u7684\u9886\u57df\u3002\n\u5728\u8fd9\u7c7b\u7a0b\u5e8f\u4e2d\uff0c\u54ea\u6015\u662f\u4e00\u70b9\u5c0f\u5c0f\u7684\u8bef\u5dee\u5728\u8ba1\u7b97\u8fc7\u7a0b\u4e2d\u8513\u5ef6\u90fd\u662f\u4e0d\u5141\u8bb8\u7684\u3002\n\u56e0\u6b64\uff0c decimal \u6a21\u5757\u4e3a\u89e3\u51b3\u8fd9\u7c7b\u95ee\u9898\u63d0\u4f9b\u4e86\u65b9\u6cd5\u3002\n\u5f53Python\u548c\u6570\u636e\u5e93\u6253\u4ea4\u9053\u7684\u65f6\u5019\u4e5f\u901a\u5e38\u4f1a\u9047\u5230 Decimal \u5bf9\u8c61\uff0c\u5e76\u4e14\uff0c\u901a\u5e38\u4e5f\u662f\u5728\u5904\u7406\u91d1\u878d\u6570\u636e\u7684\u65f6\u5019\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}